package com.wanjia.estate.service.httpservice;

import cn.hutool.json.JSONUtil;
import com.wanjia.estate.pojo.print.CommonResponse;
import com.wanjia.estate.pojo.print.DoorStatusResponse;
import com.wanjia.estate.pojo.print.PaperDrive;
import com.wanjia.estate.pojo.print.Stamp;
import com.wanjia.estate.properties.HttpRequestProperties;
import com.wanjia.estate.utils.RepeatUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PrintHttpService extends AbstractHttpService {

    @Setter(onMethod_ = @Autowired)
    private HttpRequestProperties properties;

    /**
     * 盖章
     *
     * @param request 请求参数
     * @return 响应
     */
    public Stamp.Response stamp(Stamp.Request request) {
        String url = properties.getStampUrl();
        log.debug("request url: {}", url);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<Stamp.Request> requestEntity = RequestEntity.post(url)
                .contentType(MediaType.APPLICATION_JSON)
                .body(request);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Stamp.Response response = parseObjResponseEntity(entity, Stamp.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 开启传动机构
     * @param request
     * @return
     */
    public PaperDrive.Response startPrinterPaperDrive(PaperDrive.Request request) {
        String url = properties.getStartPrinterPaperDriveUrl();
        log.debug("request url: {}", url);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(url)
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        PaperDrive.Response response = parseObjResponseEntity(entity, PaperDrive.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    public PaperDrive.Response startScannerPaperDrive(PaperDrive.Request request) {
        String url = properties.getStartScannerPaperDriveUrl();
        log.debug("request url: {}", url);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<PaperDrive.Request> requestEntity = RequestEntity.post(url)
                .contentType(MediaType.APPLICATION_JSON)
                .body(request);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        PaperDrive.Response response = parseObjResponseEntity(entity, PaperDrive.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;

    }

    /**
     * 关闭传动机构
     */
    public CommonResponse stopPrinterPaperDrive() {
        String url = properties.getStopPrinterPaperDriveUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;

    }

    public CommonResponse stopScannerPaperDrive() {
        String url = properties.getStopScannerPaperDriveUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 打开出纸门
     * @return
     */
    public CommonResponse openPaperDoor() {
        String url = properties.getOpenPaperDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    public CommonResponse repeatOpenPaperDoor() {
        return this.openPaperDoor();
        /*return RepeatUtil.repeat(3, true, this::openPaperDoor, response -> {
            if (!response.isSuccessful()) {
                return false;
            }

            DoorStatusResponse statusResponse = this.statusPaperDoor();
            if (!statusResponse.isSuccessful()) {
                return false;
            }

            DoorStatusResponse.DoorStatus status = statusResponse.getData();
            return !status.isClosed();
        });*/
    }

    /**
     * 打开印章门
     * @return
     */
    public CommonResponse openSealDoor() {
        String url = properties.getOpenSealDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    public CommonResponse openPaperOutDoor() {
        String url = properties.getOpenPaperOutDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     *
     * 查询出纸门状态
     * @return
     */
    public DoorStatusResponse statusPaperDoor() {
        String url = properties.getStatusPaperDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        DoorStatusResponse response = parseObjResponseEntity2(entity, DoorStatusResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     *
     * 查询印章门状态
     * @return
     */
    public DoorStatusResponse statusSealDoor() {
        String url = properties.getStatusSealDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        DoorStatusResponse response = parseObjResponseEntity2(entity, DoorStatusResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    public DoorStatusResponse statusPaperOutDoor() {
        String url = properties.getStatusPaperOutDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        DoorStatusResponse response = parseObjResponseEntity2(entity, DoorStatusResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

}
